﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using Facebook;

namespace Project.Models
{
    public class FacebookEntity
    {

        #region "Credenciales de FACEBOOK"
        public string app_id = "495858553782220";
        public string app_secret = "703e951937f7ef3d374a43a24b119002";
      public   string scope = "publish_stream,manage_pages";
        #endregion

 
  
   private static int arrendadorid;
   private static string nombre;
   private static string username;
 
   private static string fbId;
      public int LoginSucceded(string accessToken)
      {
          Conexion con = new Conexion();

          var fb = new FacebookClient(accessToken);
       
         
         //  fb.GetCompleted += fb_GetCompleted;
          fb.GetCompleted += (o, e) =>
          {
              if (e.Error != null)
              {
                
                  return;
              }
            
              var result = (IDictionary<string, object>)e.GetResultData();

              username = (string)result["username"];
              nombre = (string)result["first_name"] + " " + (string)result["last_name"];
              fbId=(string)result["id"];
       
 
          };
          fb.GetAsync("me");
  
       arrendadorid = con.RegistrarViaFacebook(username,nombre,accessToken,fbId);
     
       return arrendadorid;
      }

      public int AsocciarSucceded(string accessToken, int idArrendedor)
      {
          Conexion con = new Conexion();

          var fb = new FacebookClient(accessToken);


          //  fb.GetCompleted += fb_GetCompleted;
          fb.GetCompleted += (o, e) =>
          {
              if (e.Error != null)
              {

                  return;
              }

              var result = (IDictionary<string, object>)e.GetResultData();

              username = (string)result["username"];
              nombre = (string)result["first_name"] + " " + (string)result["last_name"];
              fbId = (string)result["id"];


          };
          fb.GetAsync("me");

          int Result = con.AsociarFacebook(username, nombre, accessToken, fbId, idArrendedor);
         // int Result = con.RegistrarViaFacebook(username, nombre, accessToken, fbId);

          return Result;
      }
    }


}
